//
//  TimePickerView.m
//  BuyWhat
//
//  Created by ios on 2016/11/25.
//  Copyright © 2016年 吕金状. All rights reserved.
//

#import "TimePickerView.h"

@interface TimePickerView ()<UIPickerViewDelegate , UIPickerViewDataSource>
{
    NSString *destDateString;
    NSString *time1;
    NSString *time2;
    NSString *time3;
    NSString *time4;
}

@property (nonatomic , strong) UIButton *determineButton; /**<确定*/
@property (nonatomic , strong) UIButton *cancelButton; /**<取消*/
@property (nonatomic , strong) UIPickerView *datePicker;     /**<时间选择器*/
@property (nonatomic , strong) UIView *bgView; /**<*/
@property (nonatomic , strong) NSMutableArray *hoursArray; /**<*/
@property (nonatomic , strong) NSMutableArray *minutesArray; /**<*/

@end

@implementation TimePickerView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 200 / WIDTH_5S_SCALE, SCREEN_WIDTH, 200 / WIDTH_5S_SCALE)];
        _bgView.backgroundColor = getColor(whiteColor);
        [self addSubview:backButton];
        [self addSubview:_bgView];
        [_bgView addSubview:self.datePicker];
        [_bgView addSubview:self.determineButton];
        [_bgView addSubview:self.cancelButton];
       
        [self layoutFrame];
    }
    return self;
}

- (void)setTimeString:(NSString *)timeString{
    _timeString = timeString;
 
    time1 = [self.timeString substringWithRange:NSMakeRange(0, 2)];
    time2 = [self.timeString substringWithRange:NSMakeRange(3, 2)];
    time3 = [self.timeString substringWithRange:NSMakeRange(7, 2)];
    time4 = [self.timeString substringWithRange:NSMakeRange(10, 2)];
    NSLog(@"%@%@--%@%@",time1,time2,time3,time4);
    [self.datePicker selectRow:[time1 intValue] inComponent:1 animated:YES];
    [self.datePicker selectRow:[time2 intValue] inComponent:2 animated:YES];
    [self.datePicker selectRow:[time3 intValue] inComponent:4 animated:YES];
    [self.datePicker selectRow:[time4 intValue] inComponent:5 animated:YES];
}

#pragma mark - init

- (UIButton *)determineButton{
    if (!_determineButton) {
        _determineButton = [[UIButton alloc]init];
        _determineButton.titleLabel.font = DEF_FontSize_13;
        [_determineButton setTitle:@"确定" forState:UIControlStateNormal];
        _determineButton.layer.masksToBounds = YES;
        _determineButton.layer.cornerRadius = 5;
        _determineButton.layer.borderColor = getColor(blueColor).CGColor;
        _determineButton.layer.borderWidth = 1;
        [_determineButton setTitleColor:getColor(blueColor) forState:UIControlStateNormal];
        [_determineButton addTarget:self action:@selector(determineButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _determineButton;
}

- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]init];
        _cancelButton.titleLabel.font = DEF_FontSize_13;
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        _cancelButton.layer.masksToBounds = YES;
        _cancelButton.layer.cornerRadius = 5;
        _cancelButton.layer.borderColor = getColor(blueColor).CGColor;
        _cancelButton.layer.borderWidth = 1;
        [_cancelButton setTitleColor:getColor(blueColor) forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

/**
 *  时间选择器
 */
- (UIPickerView *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIPickerView alloc]init];
        // 显示选中框
        _datePicker.showsSelectionIndicator=YES;
        _datePicker.dataSource = self;
        _datePicker.delegate = self;
    }
    return _datePicker;
}

#pragma mark - Click

- (void)determineButtonClick{
    NSLog(@"确定");
    destDateString = [NSString stringWithFormat:@"%@:%@--%@:%@",time1,time2,time3,time4];
    if (self.getTime) {
        self.getTime(destDateString);
    }
    [self dismiss];
}

#pragma  mark - <UIPickerViewDelegate ,

//// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    NSArray *array = @[@.2,@.1,@.1,@.2,@.1,@.1];
    return SCREEN_WIDTH * [array[component] floatValue];
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 1 ) {
        time1 = [self.hoursArray objectAtIndex:row];
        ;
    } else if(component == 2 ) {
        time2 = [self.minutesArray objectAtIndex:row];
 
    }else if (component == 4) {
        time3 = [self.hoursArray objectAtIndex:row];
        ;
    } else if(component == 5) {
        time4 = [self.minutesArray objectAtIndex:row];
        
    }
    
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 1  || component == 4) {
        return [self.hoursArray objectAtIndex:row];
    } else if(component == 2  || component == 5){
        return [self.minutesArray objectAtIndex:row];
        
    }else if (component == 0) {
        return @"开始时间";
    }else{
         return @"结束时间";
    }
}

#pragma  mark - <UIPickerViewDataSource>

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    NSArray *array = @[@.2,@.1,@.1,@.2,@.1,@.1];
    UILabel *myView = nil;
    myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH * [array[component] floatValue], 30)];
    myView.textAlignment = NSTextAlignmentCenter;
    myView.font = DEF_FontSize_14;         //用label来设置字体大小
    myView.backgroundColor = [UIColor clearColor];
    if (component == 0) {
    myView.text = @"开始时间";
        
    }else if (component == 1 || component == 4){
         myView.text = self.hoursArray[row];
        myView.font = DEF_FontSize_17;
    }else if (component == 2 || component == 5){
        myView.text = self.minutesArray[row];
        myView.font = DEF_FontSize_17;
    }else{
        myView.text = @"结束时间";
    }
    
    return myView;
    
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 6;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 1  || component == 4 ) {
        return self.hoursArray.count;
    } else if(component == 2  || component == 5){
        return self.minutesArray.count;
    }else{
        return 1;
    }

}

#pragma mark - frame

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bgView).with.offset(0);
        make.left.equalTo(weakSelf.bgView).with.offset(0  / WIDTH_5S_SCALE);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(200 / WIDTH_5S_SCALE);
    }];
    
    [self.determineButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bgView).with.offset(10);
        make.right.equalTo(weakSelf.bgView).with.offset( -10 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(50 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(25 / WIDTH_5S_SCALE);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bgView).with.offset(10);
        make.left.equalTo(weakSelf.bgView).with.offset(10 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(50 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(25 / WIDTH_5S_SCALE);
    }];
}

- (NSMutableArray *)hoursArray{
    if (!_hoursArray) {
         _hoursArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 24; i ++) {
            [_hoursArray addObject:[NSString stringWithFormat:@"%.2d",i]];
        }
    }
    return _hoursArray;
}

- (NSMutableArray *)minutesArray{
    if (!_minutesArray) {
        _minutesArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 60; i ++) {
            [_minutesArray addObject:[NSString stringWithFormat:@"%.2d",i]];
        }
    }
    return _minutesArray;
}

//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
